package com.jiangjiang.eshop.commodity.state;

import com.jiangjiang.eshop.commodity.constant.ApproveResult;
import com.jiangjiang.eshop.commodity.dto.GoodsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName StateManager
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/17 10:36
 * @Version 1.0
 **/
@Component
public class GoodsStateManagerImpl implements GoodsStateManager {
    @Autowired
    private GoodsStateFactory goodsStateFactory;

    /**
     * 待审核状态
     */
    @Autowired
    private WaitForApproveGoodsState waitForApproveGoodsState;
    /**
     * 待上架状态
     */
    @Autowired
    private WaitForPutOnShelvesGoodsState waitForPutOnShelvesGoodsState;
    /**
     * 审核未通过状态
     */
    @Autowired
    private ApproveRejectGoodsState approveRejectGoodsState;
    /**
     * 已上架状态
     */
    @Autowired
    private PuttedOnShelvesGoodsState puttedOnShelvesGoodsState;


    @Override
    public void create(GoodsDTO goodsDTO) {
        waitForApproveGoodsState.doTransition(goodsDTO);
    }

    @Override
    public Boolean canEdit(GoodsDTO goodsDTO) {
        GoodsState goodsState = goodsStateFactory.get(goodsDTO);
        return goodsState.canEdit(goodsDTO);
    }

    @Override
    public void edit(GoodsDTO goodsDTO) {
        waitForApproveGoodsState.doTransition(goodsDTO);
    }

    @Override
    public Boolean canApprove(GoodsDTO goodsDTO) {
        GoodsState goodsState = goodsStateFactory.get(goodsDTO);
        return goodsState.canApprove(goodsDTO);
    }

    @Override
    public void approve(GoodsDTO goods, Integer approveResult) throws Exception {
        if(ApproveResult.APPROVE_PASS.equals(approveResult)){
            waitForPutOnShelvesGoodsState.doTransition(goods);
        }else if(ApproveResult.APPROVE_REJECT.equals(approveResult)){
            approveRejectGoodsState.doTransition(goods);
        }
    }

    @Override
    public Boolean canPutOnShelves(GoodsDTO goodsDTO) {
        GoodsState goodsState = goodsStateFactory.get(goodsDTO);
        return goodsState.canPutOnShelves(goodsDTO);
    }

    @Override
    public void putOnShelves(GoodsDTO goodsDTO) {
        puttedOnShelvesGoodsState.doTransition(goodsDTO);
    }

    @Override
    public Boolean canPullOffShelves(GoodsDTO goodsDTO) {
        GoodsState goodsState = goodsStateFactory.get(goodsDTO);
        return goodsState.canPullOffShelves(goodsDTO);
    }

    @Override
    public void pullOffShelves(GoodsDTO goodsDTO) {
        waitForPutOnShelvesGoodsState.doTransition(goodsDTO);
    }

    @Override
    public Boolean canRemove(GoodsDTO goodsDTO) {
        GoodsState goodsState = goodsStateFactory.get(goodsDTO);
        return goodsState.canRemove(goodsDTO);

    }
}
